package com.dqyt.khjc.mongo;

import com.dqyt.app.bean.Maintenance;
import com.dqyt.khjc.bean.BaseBean;
import com.dqyt.khjc.bean.Work;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.mapreduce.GroupByResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 维修记录
 */
@Component
public class WorkDao {
    /**
     * 注入MongoTemplate
     */
    @Autowired
    protected MongoTemplate mongoTemplate;

    /**
     * 根据检验员id分组统计当前每个人没有完成的作业数量,按数量降序排列userid
     */
    public List<Long> getWorkCount() {
        String[] field = {Work.USERID}; //分组的依据，根据那些参数进行分组

        Aggregation aggregation1 = Aggregation.newAggregation(
                Aggregation.match(Criteria.where(Work.STATUE).is(Work.STATUE_WKS).and(BaseBean.SYSSTATUS).is(BaseBean.SYS_STATUS_OK)),    //条件筛选    可以跟好几个条件
                Aggregation.group(field).count().as("sum"),        //分组统计收藏量    ，as 起别名
                Aggregation.sort(Sort.by(Sort.Direction.ASC, "sum"))//按照数量升序
        );

        //执行
        AggregationResults<BasicDBObject> outputTypeCount1 = mongoTemplate.aggregate(aggregation1, Work.TableName, BasicDBObject.class);

        List<Long> userids = new ArrayList<>();
        //迭代器进行迭代
        for (Iterator<BasicDBObject> iterator = outputTypeCount1.iterator(); iterator.hasNext(); ) {
            DBObject obj = iterator.next();
            // 拿出用户id
            String userId = obj.get("_id").toString();
            userids.add(Long.valueOf(userId)); // 设置id
        }
        return userids;
    }

    public void save(Work work) {
        mongoTemplate.save(work); // 保存数据
    }

    /**
     * 根据标识编号取得设备信息
     *
     * @param bsbh
     * @return
     */
    public Work getWorkByBsbh(String bsbh) {
        // 构建查询条件
        Query query = new Query();

        query = Query.query(Criteria.where("bsbh").is(bsbh));
        return mongoTemplate.findOne(query, Work.class, Work.TableName);
    }

    /**
     * 分页取得作业信息
     */
    public List<Work> getWork(Integer page, Integer size, String userId, Integer ststue) {

        // 构建查询条件
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.DESC, "addTime"));
        // 如果有用户id
        if (null != userId && !"".equals(userId) && !"null".equals(userId)) {
            query.addCriteria(Criteria.where("userId").is(userId));
        }
        // 如果有作业状态
        if (null != ststue && 0 != ststue) {
            query.addCriteria(Criteria.where("statue").is(ststue));
        }
        query.skip(page).limit(size);
        List<Work> list = mongoTemplate.find(query, Work.class, Work.TableName);

        return list; // 返回结果数据
    }

    /**
     * 分页取得作业信息
     */
    public List<Work> getWorkApp(Integer page, Integer size, Integer ststue, String erweima, String bsbh) {

        // 构建查询条件
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.DESC, "addTime"));
        //
        if (null != erweima && !"".equals(erweima) && !"null".equals(erweima)) {
            query.addCriteria(Criteria.where("erweima").is(erweima));
        }
        //
        if (null != bsbh && !"".equals(bsbh) && !"null".equals(bsbh)) {
            query.addCriteria(Criteria.where("bsbh").is(bsbh));
        }
        // 如果有作业状态
        if (null != ststue && 0 != ststue) {
            query.addCriteria(Criteria.where("statue").is(ststue));
        }
        query.skip(page).limit(size);
        List<Work> list = mongoTemplate.find(query, Work.class, Work.TableName);

        return list; // 返回结果数据
    }


    /**
     * 取得作业信息总的条数
     */
    public long getTotalWork(String userId, Integer ststue) {

        // 构建查询条件
        Query query = new Query();
        // 如果有用户id
        if (null != userId && !"".equals(userId) && !"null".equals(userId)) {
            query.addCriteria(Criteria.where("userId").is(userId));
        }
        // 如果有作业状态
        if (null != ststue && 0 != ststue) {
            query.addCriteria(Criteria.where("statue").is(ststue));
        }
        long totalSize = mongoTemplate.count(query, Work.class, Work.TableName);
        return totalSize; // 返回结果数据
    }

    public long saveZyxq(Work work) {
        // 构建查询条件
        Query query = new Query();
        query = Query.query(Criteria.where("bsbh").is(work.bsbh));
        Update update = Update.update("userId", work.userId).set("userName", work.userName);
        UpdateResult wr = mongoTemplate.updateFirst(query, update, Work.class);
        return wr.getModifiedCount();
    }

    public long wanchengZyxq(Work work) {
        // 构建查询条件
        Query query = new Query();
        query = Query.query(Criteria.where("bsbh").is(work.bsbh));
        Update update = Update.update("statue", Work.STATUE_JS);
        UpdateResult wr = mongoTemplate.updateFirst(query, update, Work.class);
        return wr.getModifiedCount();
    }

    public long feizhiZyxq(Work work) {
        // 构建查询条件
        Query query = new Query();
        query = Query.query(Criteria.where("bsbh").is(work.bsbh));
        Update update = Update.update("statue", Work.STATUE_YFZ);
        UpdateResult wr = mongoTemplate.updateFirst(query, update, Work.class);
        return wr.getModifiedCount();
    }

    public long chongzhiZyxq(Work work) {
        // 构建查询条件
        Query query = new Query();
        query = Query.query(Criteria.where("bsbh").is(work.bsbh));
        Update update = Update.update("statue", Work.STATUE_WKS);
        UpdateResult wr = mongoTemplate.updateFirst(query, update, Work.class);
        return wr.getModifiedCount();
    }

}
